package com.salesys.gr.gui;

import java.util.ArrayList;
import java.util.List;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.TreeAdapter;
import org.eclipse.swt.events.TreeEvent;
import com.salesys.gr.util.FrmHelper;
import com.salesys.gr.util.LoginedUserHelper;
import com.salesys.tony.domain.User;
import com.salesys.tony.service.UserService;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.events.TraverseEvent;

public class FrmViewRelation2 {

	protected Shell FrmViewRelation;

	// int queryId = 0;
	//
	// public int getQueryId() {
	// return queryId;
	// }
	//
	// public void setQueryId(int queryId) {
	// this.queryId = queryId;
	// }

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		FrmViewRelation.open();
		FrmViewRelation.layout();
		while (!FrmViewRelation.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 * 
	 * @wbp.parser.entryPoint
	 */
	protected void createContents() {
		FrmViewRelation = new Shell(SWT.CLOSE | SWT.APPLICATION_MODAL);
		FrmViewRelation.addTraverseListener(new TraverseListener() {
			public void keyTraversed(TraverseEvent e) {
				if (e.keyCode == 27) {
					FrmViewRelation.close();
				}
			}
		});
		FrmViewRelation.setSize(450, 250);
		FrmViewRelation.setText("查看用户关系");
		// 窗口居中
		FrmViewRelation.setLocation(new FrmHelper().setLocationCenter(FrmViewRelation));

		Tree tree = new Tree(FrmViewRelation, SWT.BORDER);
		tree.setBounds(10, 10, 199, 205);

		TreeItem trtmSelf = new TreeItem(tree, SWT.NONE);
		trtmSelf.setText(LoginedUserHelper.getLoginedUserUsername());

		final TreeItem trtmtemp = new TreeItem(trtmSelf, SWT.NONE);
		trtmtemp.setText("载入中。。。");

		final Group group = new Group(FrmViewRelation, SWT.NONE);
		group.setText("客户信息");
		group.setBounds(215, 10, 219, 205);

		Label lblNickname = new Label(group, SWT.NONE);
		lblNickname.setBounds(7, 30, 48, 17);
		lblNickname.setText("姓　名：");

		Label lblMark = new Label(group, SWT.NONE);
		lblMark.setBounds(7, 60, 43, 17);
		lblMark.setText("现　金：");

		Label lblMemberId = new Label(group, SWT.NONE);
		lblMemberId.setBounds(7, 90, 56, 17);
		lblMemberId.setText("加盟卡号：");

		Label lblQq = new Label(group, SWT.NONE);
		lblQq.setBounds(7, 120, 43, 17);
		lblQq.setText("QQ：");

		Label lblEmail = new Label(group, SWT.NONE);
		lblEmail.setBounds(7, 150, 43, 17);
		lblEmail.setText("邮　箱：");

		final Label lblNicknameData = new Label(group, SWT.NONE);
		lblNicknameData.setBounds(65, 30, 150, 17);

		final Label lblMarkData = new Label(group, SWT.NONE);
		lblMarkData.setBounds(65, 60, 150, 17);

		final Label lblMemberIdData = new Label(group, SWT.NONE);
		lblMemberIdData.setBounds(65, 90, 150, 17);

		final Label lblQqData = new Label(group, SWT.NONE);
		lblQqData.setBounds(65, 120, 150, 17);

		final Label lblEmailData = new Label(group, SWT.NONE);
		lblEmailData.setBounds(65, 150, 150, 17);

		tree.addTreeListener(new TreeAdapter() {
			@Override
			public void treeExpanded(TreeEvent e) {
				try {
					String child = new UserService().queryChildFromUsername(((TreeItem) e.item).getText());
					List<String> childList = new UserService().queryChildUsernameFromChild(child);
					List<TreeItem> trtmChild = new ArrayList<TreeItem>();
					if (childList == null) {
						(((TreeItem) e.item).getItem(0)).setText("没有下级");
					} else {
						(((TreeItem) e.item).getItem(0)).dispose();
						for (int i = 0; i < childList.size(); i++) {
							trtmChild.add(i, new TreeItem((TreeItem) e.item, SWT.NONE));
							trtmChild.get(i).setText(childList.get(i));
							TreeItem trtmTempChild = new TreeItem(trtmChild.get(i), SWT.NONE);
							trtmTempChild.setText("载入中。。。");
						}
					}
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}

			@Override
			public void treeCollapsed(TreeEvent e) {
				try {
					TreeItem Temp = (TreeItem) e.item;
					((TreeItem) e.item).removeAll();
					TreeItem trtmtemp = new TreeItem(Temp, SWT.NONE);
					trtmtemp.setText("载入中。。。");
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
		});

		tree.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try {
					if (((TreeItem) e.item).getText() != "没有下级") {
						User infoUser = new User(new UserService().queryInfoFromUsername(((TreeItem) e.item).getText()));
						if (infoUser.getNickname() != null) {
							lblNicknameData.setText(infoUser.getNickname());
						} else {
							lblNicknameData.setText("");
						}
						lblMarkData.setText(infoUser.getMark());
						if (infoUser.getMemberId() != null) {
							lblMemberIdData.setText(infoUser.getMemberId());
						} else {
							lblMemberIdData.setText("非正式会员没有会员卡号");
						}
						if (infoUser.getQq() != null) {
							lblQqData.setText(infoUser.getQq());
						} else {
							lblQqData.setText("");
						}
						if (infoUser.getEmail() != null) {
							lblEmailData.setText(infoUser.getEmail());
						} else {
							lblEmailData.setText("");
						}
					}
				} catch (Exception e2) {
					e2.printStackTrace();
				}
				
				// setQueryId(new User(new
				// UserService().queryInfoFromUsername(((TreeItem)
				// e.item).getText())).getId());
				//
				// Button button = new Button(group, SWT.NONE);
				// button.addMouseListener(new MouseAdapter() {
				// @Override
				// public void mouseUp(MouseEvent e) {
				// FrmViewMarkRecord FrmViewMarkRecord1 = new
				// FrmViewMarkRecord();
				// FrmViewMarkRecord1.transfer(getQueryId());
				// FrmViewMarkRecord1.open();
				// }
				// });
				// button.setBounds(49, 178, 120, 27);
				// button.setText("查看现金变更纪录");
			}
		});
	}
}